using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Modules.UI.Sub
{
    [CreateAssetMenu(fileName = "SubPageStore", menuName = "Scriptable Objects/Sub Page Store")]
    public class SubPageStore : ScriptableObject
    {
        [ListDrawerSettings(AlwaysAddDefaultValue = true, ShowPaging = false /*, ShowIndexLabels = true*/)]
        [SerializeField]
        private List<SubPage> pages;

        public SubPage Get(string n)
        {
            SubPage page = pages.Find(info => info.name.Equals(n));
            if (page == null)
            {
                throw new ArgumentException($"未找到名称为 '{n}' 的页面，请确保该页面已在 PageStore 中注册。");
            }

            return page;
        }

        public SubPage Get(Type type)
        {
            SubPage page = pages.Find(info => info.GetType() == type);
            if (page == null)
            {
                throw new ArgumentException($"未找到类型为 '{type.Name}' 的页面，请确保该页面已在 PageStore 中注册。");
            }

            return page;
        }

        public T Get<T>() where T : SubPage
        {
            return (T)Get(typeof(T));
        }
    }
}